<template>
  <Panel>
    <template #title>
      <p>产品分类</p>
    </template>
    <template #content>
      <div>
        <p>
          <el-button type="primary" size="mini" @click="dialogVisible = true"
            >添加分类</el-button
          >
        </p>
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="序号" type="index" width="80">
        </el-table-column>
        <el-table-column label="分类名称" width="180">
          <template v-slot="{ row }">
            <span v-if="row.flag">{{ row.cateName }}</span>
            <el-input v-else v-model="row.cateName"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" width="120">
          <template v-slot="{ row }">
            <el-switch
              :disabled="row.flag"
              :active-value="1"
              :inactive-value="0"
              v-model="row.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template v-slot="{ row }">
            <el-button
              :type="row.flag ? 'primary' : 'success'"
              size="mini"
              @click="editCate(row)"
              >{{ row.flag ? '编辑' : '完成' }}</el-button
            >
            <el-button type="danger" size="mini" @click="delCate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-dialog title="添加分类" :visible.sync="dialogVisible" width="50%">
        <div>
          <el-form
            :model="cateForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="分类名称" prop="cateName">
              <el-input v-model="cateForm.cateName"></el-input>
            </el-form-item>
            <el-form-item label="是否启用">
              <el-switch
                v-model="cateForm.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </Panel>
</template>

<script>
import { getCateList, deleteCate, addNewCate, editCate } from '../../api/pro.js'
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      dialogVisible: false,
      cateForm: {
        cateName: '',
        state: 0
      },
      rules: {
        cateName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCate()
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.getCate()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getCate()
    },
    async getCate() {
      const { data, total } = await getCateList({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      data.forEach(item => {
        item.flag = true
      })
      this.tableData = data
      this.total = total
    },
    async delCate(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await deleteCate({
            id: id
          })
          if (data.code === 0) {
            this.$message({
              type: 'success',
              message: data.msg
            })
            this.getCate()
          } else {
            this.$message({
              type: 'error',
              message: data.msg
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async editCate(row) {
      if (row.flag) {
        row.flag = false
      } else {
        const data = await editCate({
          id: row.id,
          cateName: row.cateName,
          state: !!row.state
        })
        if (data.code === 0) {
          row.flag = true
          // this.getCate()
        }
      }
    },
    async addCate() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          const data = await addNewCate(this.cateForm)
          if (data.code === 0) {
            this.$message({
              message: data.msg,
              type: 'success'
            })
            this.getCate()
            this.dialogVisible = false
          } else {
            this.$message({
              message: data.msg,
              type: 'error'
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 10px 0;
}
</style>
